﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.WorkItemTracking;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TfsBugger.Util;
using TfsBugger.Util.Ui;
using System.Net;

namespace TfsBugger.Dialogs
{
    public partial class ServerConnectDialog : Form
    {
        public ServerConnectDialog()
        {
            InitializeComponent();
        }


        #region Nested Types
        public class ProjItemWrapper
        {
            public Project Proj { get; set; }

            public override string ToString()
            {
                return Proj.Name;
            }
        }
        #endregion


        #region Fields
        private string m_serverName = "tfs06.codeplex.com";
        #endregion 


        #region Methods
        private bool Connect()
        {
            if (null == Program.TfsServer)
            {
                //var icp = new UICredentialsProvider();
                //Program.TfsServer = TeamFoundationServerFactory.GetServer(m_serverName, icp);

                //HACK: Temporary hack to avoid having to use ui credentials provider 
                //on each app run 
                NetworkCredential creds = new NetworkCredential("Zoetermeer_cp", "qytiuo", "snd");
                Program.TfsServer = new TeamFoundationServer("https://" + m_serverName + ":443", creds);
                
            }

            try
            {
                Program.TfsServer.Authenticate();
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    "A connection with the server could not be established.  Details:\n\n" 
                    + ex.Message,
                    "Connection Error",
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Error);

                return false;
            }

            return true;
        }
        #endregion


        #region Event Handlers
        private void OnFormLoad(object sender, EventArgs e)
        {
            this.txtServerName.Text = m_serverName;
        }


        private void OnConnectButtonClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtServerName.Text))
            {
                MessageBox.Show(
                    "You must enter a TFS server name.",
                    "Missing Server Name", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Asterisk);
            }

            m_serverName = this.txtServerName.Text;
            using (WaitScope wait = new WaitScope(this))
            {
                if (!Connect())
                {
                    this.btnOK.Enabled = false;
                    return;
                }

                var wiStor = (WorkItemStore)Program.TfsServer.GetService(typeof(WorkItemStore));
                foreach (Project proj in wiStor.Projects)
                    this.lstProjects.Items.Add(new ProjItemWrapper { Proj = proj }); 
            }
        }


        private void OnProjectItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.NewValue == CheckState.Checked)
            {
                this.btnOK.Enabled = true;
                return;
            }

            this.btnOK.Enabled = this.lstProjects.CheckedItems.Count > 0;
        }


        private void OnOkButtonClick(object sender, EventArgs e)
        {
            var selProjs = from i in this.lstProjects.CheckedItems.Cast<object>()
                           select ((ProjItemWrapper)i).Proj;

            Program.SelectedProjects = selProjs.ToArray();
        } 
        #endregion        
    }
}
